package fr.inria.arles.temperatureManagement.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * Activity to display application's home screen.
 * 
 * @author ajay chhatwal
 */
public class Home extends Activity {

	private String username, ID, partition;

	/** Handles event: Button viewTempB clicked */
	private final OnClickListener viewTempBClicked = new OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent(Home.this, ViewTemp.class);
			intent.putExtra("username", username);
			intent.putExtra("ID", ID);
			intent.putExtra("partition", partition);
			startActivity(intent);
		}
	};

	/** Handles event: Button changeTemp clicked */
	private final OnClickListener changeTempBClicked = new OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent(Home.this, ChangeTemp.class);
			intent.putExtra("username", username);
			intent.putExtra("ID", ID);
			intent.putExtra("partition", partition);
			startActivity(intent);
		}
	};

	/**
	 * Called when the activity is first created.
	 * 
	 * @param savedInstanceState
	 *            the saved instance state
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		Intent i = getIntent();
		username = i.getStringExtra("username");
		ID = i.getStringExtra("ID");
		partition = i.getStringExtra("partition");
		Log.d("username", username);
		Log.d("ID", ID);
		Log.d("partition", partition);

		if (username != null) {
			TextView welcomeTV = (TextView) findViewById(R.id.welcomeHomeTV);
			welcomeTV.setText("Welcome " + username);
		}
		if (partition != null) {
			TextView partitionTV = (TextView) findViewById(R.id.partitionHomeTV);
			partitionTV.setText(partition);
		}

		Button viewTempB = (Button) findViewById(R.id.viewTempB);
		viewTempB.setOnClickListener(viewTempBClicked);
		Button changeTempB = (Button) findViewById(R.id.changeTempB);
		changeTempB.setOnClickListener(changeTempBClicked);

	}

	/**
	 * Called when the activity is first created.
	 * 
	 * @param menu
	 *            the Menu to be populated
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	/**
	 * Handles item selections
	 * 
	 * @param item
	 *            the selected MenuItem
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			Intent intent = new Intent(this, Settings.class);
			startActivity(intent);
			return true;
		case R.id.exit:
			moveTaskToBack(true);
			return true;
		}
		return false;
	}

}